package com.smartens.boit.util;

import android.util.Log;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.smartens.boit.bean.AddDeviceGataWayJson;
import com.smartens.boit.bean.BaseData;

import com.smartens.boit.bean.DeviceListData;
import com.smartens.boit.bean.DevicesList;
import com.smartens.boit.bean.ElectricBean;
import com.smartens.boit.bean.EnergyConsumptionBean;
import com.smartens.boit.bean.GetDeviceGateWayDetailResult;
import com.smartens.boit.bean.LightDetailBean;
import com.smartens.boit.bean.ListBean;
import com.smartens.boit.bean.ListBeans;
import com.smartens.boit.bean.ListParameterBean;
import com.smartens.boit.bean.LoginBean;
import com.smartens.boit.bean.ProcessedBean;
import com.smartens.boit.bean.OffLineBean;
import com.smartens.boit.bean.RecordDetail;
import com.smartens.boit.bean.StatusDetailBean;
import com.smartens.boit.bean.RecordeDetail_datd;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


/**
 * @author tianyu
 */
public class HttpHelper {

    private static final String URL_INNER = "http://101.200.61.54:6200";//http://101.200.61.54:6200
    private static final String URL_OUTER = "http://d.meter-ocr.smartens-tek.com/api";
    public static final MediaType JSON
            = MediaType.get("application/json; charset=utf-8");
    private static final String URL_COLLECT = "/collection/collect";
    private static final String URL_LOGIN = "/LedService.svc/UserLogin";
    private static final String URL_UPDATE_WAY = "/LedService.svc/UpdateOpenModeGataWay";
    private static final String URL_DETAIL = "/LedService.svc/GetProjectDetail";
    private static final String URL_LIST = "/LedService.svc/GetProjectList";
    private static final String URL_GROUP_LIGHT = "/LedService.svc/SetGroupLight";
    private static final String URL_AUTO = "/LedService.svc/EditDeviceGateWayOn";
    private static final String URL_DEVICE_STATUS = "/LedService.svc/GetDeviceStatusHistoryList";
    private static final String URL_GROUP_WAY = "/LedService.svc/EditDeviceGroupLight";
    private static final String URL_ADDPROJECT = "/LedService.svc/AddProject";
    private static final String URL_Energy = "/LedService.svc/GetDeviceEneygyList";
    private static final String URL_OFFLINE = "/LedService.svc/GetDevicesList";
    private static final String URL_Devices_List = "/LedService.svc/GetDevicesList";
    private static final String URL_FAULT = "/LedService.svc/GetDeviceFaultList";
    private static final String URL_GroupList = "/LedService.svc/GetDeviceGroupList";
    private static final String URL_GetDeviceStatusListResult = "/LedService.svc/GetDeviceStatusList";
    public static final String URL_GetDeviceOnOffDetailResult = "/LedService.svc/GetDeviceOnOffDetail";
    public static final String URL_GetUpdateOpenModeGataWayResult = "/LedService.svc/UpdateOpenModeGataWay";
    public  static  final String URL_AddDeviceGateWayResult="/LedService.svc/AddDeviceGateWay";
    public  static  final String URL_GetDeviceGateWayDetail="/LedService.svc/GetDeviceGateWayDetail";
    private static final String CODE_SUCCESS = "1";
    private static String URL = URL_INNER;

    private static HttpHelper instance;

    public static HttpHelper getInstance() {
        if (instance == null) {
            synchronized (HttpHelper.class) {
                if (instance == null) {
                    instance = new HttpHelper();
                }
            }
        }
        return instance;
    }

    private OkHttpClient client;
    private Gson gson;

    private HttpHelper() {
        client = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .build();
        gson = new Gson();
    }
    // 添加设备
    private   void AddDeviceGateWay(GetDeviceGateWayDetailResult getDeviceGateWayDetailResult,final HttpCallBack callback)
    {
        AddDeviceGataWayJson addDeviceGataWayJson=new AddDeviceGataWayJson(getDeviceGateWayDetailResult);
        Gson gson=new Gson();
        JsonObject jsonObject=gson.toJsonTree(getDeviceGateWayDetailResult).getAsJsonObject();
        JsonObject jsonobject1=new JsonObject();
        jsonobject1.addProperty("obj",jsonObject.toString());
        String jsonString=jsonobject1.toString();
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_AddDeviceGateWayResult, body, callback, "AddDeviceGateWayResult");
    }
    public  void GetDeviceGateWayDetail(String mac ,final HttpCallBack<GetDeviceGateWayDetailResult> callback)
    {
      //  obj: "{\"getwayMac\":\"37FFD4054155353518890757\"}"
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("getwayMac",mac);
        JsonObject jsonObject1=new JsonObject();
        jsonObject1.addProperty("obj",jsonObject.toString());
        Log.v("dsfsdfsdf",jsonObject1.toString());
        String jsonString=jsonObject1.toString();
        RequestBody body = RequestBody.create(JSON, jsonString);
        post2(URL_GetDeviceGateWayDetail, body, callback,GetDeviceGateWayDetailResult.class,"GetDeviceGateWayDetailResult");
    }
    //{obj: "{"getwayMac":"32FFD7055643373914721851","Type":2,"PageIndex":1,"PageSize":50}"}
    public void GetDeviceGroupList(String getwayMac, final HttpCallBack<RecordeDetail_datd> callback) {
        String jsonString = "{\"obj\":\"{\\\"getwayMac\\\":\\\"" + getwayMac + "\\\",\\\"Type\\\":2,\\\"PageIndex\\\":1,\\\"PageSize\\\":50}\"}";
        Log.v("asdsadsada", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_GroupList, body, callback, RecordeDetail_datd.class, "GetDeviceGroupListResult");
    }

    public void login(String name, String pwd, final HttpCallBack<LoginBean> callback) {

        String jsonString = "{\"obj\":\"{\\\"UserName\\\":\\\"" + name + "\\\",\\\"PassWord\\\":\\\"" + pwd + "\\\"}\"}";
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_LOGIN, body, callback, LoginBean.class, "UserLoginResult");
    }

    /**
     * 获取实时电流值
     * 当前电流值和上一次电流值差值>0.08 为异常
     *
     * @param date     当前日期
     * @param callback
     */
    public void GetDeviceStatusHistoryList(String date, final HttpCallBack<ElectricBean> callback) {
        String jsonString = "{\"obj\":\"{\\\"deviceMac\\\":\\\"32FFD7055643373914721851\\\",\\\"TimeSpan\\\":\\\"" + date + "\\\"}\"}";
        Log.d("HttpHelper", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_DEVICE_STATUS, body, callback, ElectricBean.class, "GetDeviceStatusHistoryListResult");

    }

    /**
     * 获取调光方案详情
     *
     * @param Projectid 31204b3a-abf9-4d79-b282-764b017480b3 fa03890b-6d9e-4161-9b89-edf8b37595d0
     * @param callback
     */
    public void GetProjectDetailResult(String Projectid, final HttpCallBack<LightDetailBean> callback) {
        String jsonString = "{\"obj\":\"{\\\"Projectid\\\":\\\"" + Projectid + "\\\"}\"}";
        Log.d("HttpHelper", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_DETAIL, body, callback, LightDetailBean.class, "GetProjectDetailResult");
    }


    /**
     * 获取调光方案列表
     * 调光方案
     *
     * @param callback
     */
    public void GetProjectList(final HttpCallBack<ListBeans> callback) {
        String jsonString = "{\"obj\":\"{\\\"PageIndex\\\":1,\\\"PageSize\\\":99999,\\\"Coustomers\\\":\\\"\\\",\\\"Type\\\":2}\"}";
        Log.d("HttpHelper", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_LIST, body, callback, ListBeans.class, "GetProjectListResult");
    }

    /**
     * 获取已处理列表
     *
     * @param callback
     */
    public void GetProcessedList(final HttpCallBack<ProcessedBean> callback) {
        String jsonString = "{\"obj\":\"{\\\"UserNum\\\":\\\"001\\\",\\\"Status\\\":\\\"3\\\",\\\"keywords\\\":\\\"\\\",\\\"locate\\\":\\\"\\\",\\\"PageIndex\\\":1,\\\"PageSize\\\":10}\"}";
        Log.d("HttpHelper", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_FAULT, body, callback, ProcessedBean.class, "GetDeviceFaultListResult");
    }

    /**
     * 离线设备
     *
     * @param status       1： 正常设备  3：离线设备
     * @param httpCallBack
     */
    public void GetOffLineList(String status, final HttpCallBack<OffLineBean> httpCallBack) {
        String jsonString = "{\"obj\":\"{\\\"UserNum\\\":\\\"001\\\",\\\"deviceNum\\\":\\\"\\\",\\\"keywords\\\":\\\"\\\",\\\"Status\\\":\\\"" + status + "\\\",\\\"unitNum\\\":\\\"\\\",\\\"locate\\\":\\\"\\\",\\\"PageIndex\\\":1,\\\"PageSize\\\":10,\\\"alertMode\\\":-1}\"}";
        Log.d("请求结果", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_OFFLINE, body, httpCallBack, OffLineBean.class, "GetDevicesListResult");
    }


    public void GetDeviceStatusListResult(final HttpCallBack<StatusDetailBean> callback) {
        String jsonString = "{\"obj\":\"{\\\"UserNum\\\":\\\"001\\\",\\\"deviceMac\\\":\\\"32FFD7055643373914721851\\\",\\\"keywords\\\":\\\"\\\",\\\"Status\\\":-1,\\\"unitNum\\\":\\\"\\\",\\\"locate\\\":\\\"\\\",\\\"PageIndex\\\":1,\\\"PageSize\\\":10}\"}";
        Log.d("请求结果", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_GetDeviceStatusListResult, body, callback, StatusDetailBean.class, "GetDeviceStatusListResult");

    }

    public void GetDeviceStatusListResult2(final HttpCallBack<StatusDetailBean> callback) {
        String jsonString = "{\"obj\":\"{\\\"UserNum\\\":\\\"001\\\",\\\"deviceNum\\\":\\\"\\\",\\\"keywords\\\":\\\"\\\",\\\"Status\\\":-1,\\\"unitNum\\\":\\\"\\\",\\\"locate\\\":\\\"\\\",\\\"PageIndex\\\":1,\\\"PageSize\\\":10}\"}";
        Log.d("请求结果", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_GetDeviceStatusListResult, body, callback, StatusDetailBean.class, "GetDeviceStatusListResult");

    }

    /**
     * 开启自动模式
     *
     * @param openTime
     * @param closeTime
     * @param projectID
     * @param callback
     */

    public void EditDeviceGateWayOn(String openTime, String closeTime, String projectID, final HttpCallBack<String> callback) {
        String jsonString =
                "{\"obj\":\"{\\\"DeviceMac\\\": \\\"32FFD7055643373914721851\\\",\\\"Deviceclosetime1\\\": \\\"" + closeTime + "\\\",\\\"Deviceclosetime2\\\": \\\"\\\",\\\"Deviceclosetime3\\\": \\\"\\\",\\\"Deviceopentime1\\\": \\\"" + openTime + "\\\",\\\"Deviceopentime2\\\": \\\"\\\",\\\"Deviceopentime3\\\": \\\"\\\",\\\"ProjectID\\\"" + projectID + "\\\"\\\",\\\"ProjectIDB\\\":\\\"\\\",\\\"ProjectIDC\\\":\\\"\\\",\\\"open_device_sunset\\\":\\\"0\\\",\\\"open_device_sunsetB\\\":\\\"0\\\",\\\"open_device_sunsetC\\\":\\\"0\\\"}\"}";
        Log.d("HttpHelper", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        put(URL_AUTO, body, callback, "EditDeviceGateWayOnResult");
    }

    /**
     * 添加修改方案
     *
     * @param ProjectID                 31204b3a-abf9-4d79-b282-764b017480b3
     * @param ProjectName               0058
     * @param DimmingMode               0是突变 1是渐变
     * @param "DeviceType":"1"//固定传1
     * @param "ProjectType":"2"//1是控制方案 2是调光
     * @param list
     */
    public void AddProject(String ProjectID, String ProjectName, String DimmingMode, List<ListParameterBean> list, final HttpCallBack<String> callback) {
        String jsonString = "{\"obj\":\"{\\\"ProjectID\\\":\\\"" + ProjectID + "\\\",\\\"ProjectName\\\":\\\"" + ProjectName + "\\\",\\\"DimmingMode\\\":\\\"" + DimmingMode + "\\\",\\\"DeviceType\\\":\\\"1\\\",\\\"ProjectType\\\":\\\"2\\\",\\\"Coustomers\\\":\\\"\\\",\\\"ListParameter\\\":[{\\\"Time\\\":\\\"" + list.get(0).getTime() + "\\\",\\\"Brightness\\\":\\\"" + list.get(0).getBrightness() + "\\\",\\\"Sort\\\":1,\\\"Closetime\\\":\\\"00:00\\\"},{\\\"Time\\\":\\\"" + list.get(1).getTime() + "\\\",\\\"Brightness\\\":\\\"" + list.get(1).getBrightness() + "\\\",\\\"Sort\\\":2,\\\"Closetime\\\":\\\"00:00\\\"},{\\\"Time\\\":\\\"" + list.get(2).getTime() + "\\\",\\\"Brightness\\\":\\\"" + list.get(2).getBrightness() + "\\\",\\\"Sort\\\":3,\\\"Closetime\\\":\\\"00:00\\\"},{\\\"Time\\\":\\\"25:00\\\",\\\"Brightness\\\":\\\"0\\\",\\\"Sort\\\":4,\\\"Closetime\\\":\\\"00:00\\\"}]}\"}";
        Log.d("HttpHelper", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        put(URL_ADDPROJECT, body, callback, "AddProjectResult");
    }

    /**
     * 分组调光方案
     *
     * @param GatewayMac
     * @param GroupNum
     * @param ProjectID
     * @param callback
     */
    public void EditDeviceGroupLight(String GatewayMac, String GroupNum, String ProjectID, final HttpCallBack<String> callback) {
        String jsonString = "{\"obj\":\"{\\\"GatewayMac\\\":\\\"32FFD7055643373914721851\\\",\\\"GroupNum\\\":\\\"" + GroupNum + "\\\",\\\"ProjectID\\\":\\\"" + ProjectID + "\\\"}\"}";
        Log.d("HttpHelper", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        put(URL_GROUP_WAY, body, callback, "EditDeviceGroupLightResult");
    }

    /**
     * 调光
     *
     * @param GroupNumber
     * @param DimmingValue
     * @param callback
     */
    public void SetGroupLight(String GroupNumber, String DimmingValue, final HttpCallBack<String> callback) {
        String jsonString = "{\"obj\":\"{\\\"DeviceMac\\\":\\\"32FFD7055643373914721851\\\",\\\"GroupNumber\\\":\\\"" + GroupNumber + "\\\",\\\"DimmingValue\\\":\\\"" + DimmingValue + "\\\"}\"}";
        Log.d("HttpHelperaaa", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        put(URL_GROUP_LIGHT, body, callback, "SetGroupLightResult");
    }

    /**
     * 开启手动模式
     *
     * @param getwayMac
     * @param control_mode
     * @param callback
     */

    public void UpdateOpenModeGataWay(String getwayMac, String control_mode, final HttpCallBack<String> callback) {
        String jsonString = "{\"obj\":\"{\\\"getwayMac\\\":\\\"" + getwayMac + "\\\",\\\"control_mode\\\":\\\"" + control_mode + "\\\"}\"}";
        Log.d("HttpHelper", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        put(URL_UPDATE_WAY, body, callback, "UpdateOpenModeGataWayResult");
    }

    /**
     * 获取能耗
     *
     * @param callback
     */
    public void GetDeviceEneygyList(final HttpCallBack<EnergyConsumptionBean> callback) {
        String jsonString = "{\"obj\":\"{\\\"deviceMac\\\":\\\"32FFD7055643373914721851\\\",\\\"Type\\\":2}\"}";
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_Energy, body, callback, EnergyConsumptionBean.class, "GetDeviceEneygyListResult");
    }


    /**
     * 故障历史列表
     *
     * @param callback
     */
    public void GetDeviceFaultList(final HttpCallBack<ListBean> callback) {
        String jsonString = "{\"obj\": \"{\\\"UserNum\\\":\\\"001\\\",\\\"Status\\\":\\\"3\\\",\\\"keywords\\\":\\\"\\\",\\\"locate\\\":\\\"\\\",\\\"PageIndex\\\":1,\\\"PageSize\\\":10}\"}";
        Log.d("HttpHelper", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_FAULT, body, callback, ListBean.class, "GetDeviceFaultListResult");
    }

    /**
     * post 请求
     *
     * @param url      url地址
     * @param body     参数体
     * @param callback
     * @param myType
     * @param key      想要取值的key
     * @param <T>
     */
    private <T> void post(final String url, RequestBody body, final HttpCallBack<T> callback, Class<T> myType, String key) {
        Request.Builder builder = new Request.Builder();

        Request request = builder
                .url(URL + url)
                .post(body)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                callback.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                try {
                    String result = Objects.requireNonNull(response.body()).string();
                    Log.d("HttpHelper " + url, result);
                    JsonObject returnData = new JsonParser().parse(result).getAsJsonObject();
                    Log.e("key key key", key);
                    JsonObject value = returnData.getAsJsonObject(key);
                    Log.v("dfsdfsfsdf", value.toString());
                    if (value.equals(null)) {
                        return;
                    } else {

                        BaseData<T> r = gson.fromJson(value, TypeToken.getParameterized(BaseData.class, myType).getType());
                        if (CODE_SUCCESS.equalsIgnoreCase(r.getRecordStatus())) {
                            callback.onSuccess(r.getRecordDetail());
                        } else {
                            callback.onFailure(r.getRecordRemark());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    callback.onFailure(e.getMessage());
                }
            }
        });
    }

    private <T> void post2(final String url, RequestBody body, final HttpCallBack<T> callback, Class<T> myType, String key) {
        Request.Builder builder = new Request.Builder();

        Request request = builder
                .url(URL + url)
                .post(body)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                callback.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                try {
                    String result = Objects.requireNonNull(response.body()).string();
                    Log.d("HttpHelper " + url, result);
                    JsonObject returnData = new JsonParser().parse(result).getAsJsonObject();
                    Log.e("key key key", key);
                    JsonObject value = returnData.getAsJsonObject(key);
                    Log.v("dfsdfsfsdf", value.toString());
                    if (value.equals(null)) {
                        return;
                    } else {

                        BaseData<T> r = gson.fromJson(value, TypeToken.getParameterized(BaseData.class, myType).getType());
                        if (CODE_SUCCESS.equalsIgnoreCase(r.getRecordStatus())) {
                            callback.onSuccess(r.getRecordDetail());
                        } else {
                            callback.onFailure(r.getRecordRemark());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    callback.onFailure(e.getMessage());
                }
            }
        });
    }

    //(final String url, RequestBody body, final HttpCallBack<T> callback, Class<T> myType, String key
    private void post(final String url, RequestBody body, final HttpCallBack callback, String key) {
        post(url, body, callback, Object.class, key);
    }

    private <T> void post(final String url, RequestBody body, final HttpCallBackClass<T> callback, Class<T> myType) {
        Request.Builder builder = new Request.Builder();

        Request request = builder
                .url(URL + url)
                .post(body)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                callback.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                try {
                    String result = Objects.requireNonNull(response.body()).string();
                    Log.d("HttpHelper " + url, result);
                    JsonObject value = new JsonParser().parse(result).getAsJsonObject();
                    Log.v("dfsdfsfsdf", value.toString());
                    if (value.equals(null)) {
                        return;
                    } else {
                        T r = gson.fromJson(value, myType);
                        callback.onSuccess(r);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    callback.onFailure(e.getMessage());
                }
            }
        });
    }

    private <T> void put(final String url, RequestBody body, final HttpCallBack<T> callback, String key) {
        Request.Builder builder = new Request.Builder();

        Request request = builder
                .url(URL + url)
                .post(body)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                callback.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                try {
                    String result = Objects.requireNonNull(response.body()).string();
                    Log.d("HttpHelper " + url, result);
                    JsonObject returnData = new JsonParser().parse(result).getAsJsonObject();
                    JsonObject value = returnData.getAsJsonObject(key);
                    if (value.equals(null)) {
                        return;
                    } else {
                        BaseData<T> r = gson.fromJson(value, new TypeToken<BaseData<T>>() {
                        }.getType());
                        List<String> list1 = new ArrayList<>();
                        list1.add(r.getRecordRemark());
                        if (CODE_SUCCESS.equalsIgnoreCase(r.getRecordStatus())) {
                            callback.onSuccess((List<T>) list1);
                        } else {
                            callback.onFailure(r.getRecordRemark());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    callback.onFailure(e.getMessage());
                }
            }
        });
    }

    public static void changeEnv(NetEnv env) {
        switch (env) {
            case INNER:
                URL = URL_INNER;
                break;
            case OUTER:
            default:
                URL = URL_OUTER;
                break;
        }
    }


    public interface HttpCallBack<T> {
        /**
         * 接口成功回调
         *
         * @param result 成功回调
         */
        void onSuccess(List<T> result);

        /**
         * 接口失败回调
         *
         * @param message 错误提示
         */
        void onFailure(String message);
    }

    public interface HttpCallBackClass<T> {
        /**
         * 接口成功回调
         *
         * @param result 成功回调
         */
        void onSuccess(T result);

        /**
         * 接口失败回调
         *
         * @param message 错误提示
         */
        void onFailure(String message);
    }


    /**
     * 获取设备列表
     *
     * @param Status 1：正常  3：离线
     */
    public void GetDevicesList(int Status, final HttpCallBack<DevicesList> callback) {
        //{"UserNum":"001","deviceNum":"","keywords":"","Status":-1,"unitNum":"","locate":"","PageIndex":1,"PageSize":10,"alertMode":-1}
        String jsonString = "{\"obj\":\"{\\\"UserNum\\\":\\\"001\\\",\\\"deviceNum\\\":\\\"\\\",\\\"keywords\\\":\\\"\\\",\\\"Status\\\":\\\"" + Status + "\\\",\\\"unitNum\\\":\\\"\\\",\\\"locate\\\":\\\"\\\",\\\"PageIndex\\\":1,\\\"PageSize\\\":10,\\\"alertMode\\\":-1}\"}";
        Log.d("HttpHelper", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_Devices_List, body, callback, DevicesList.class, "GetDevicesListResult");
    }

    public void GetAllDevicesList(final  HttpCallBackClass<DeviceListData>callBackClass) {
        String jsonString = "{\"obj\":\"{\\\"UserNum\\\":\\\"001\\\",\\\"deviceNum\\\":\\\"\\\",\\\"keywords\\\":\\\"\\\",\\\"Status\\\":\\\"" + -1 + "\\\",\\\"unitNum\\\":\\\"\\\",\\\"locate\\\":\\\"\\\",\\\"PageIndex\\\":1,\\\"PageSize\\\":10,\\\"alertMode\\\":-1}\"}";
        Log.d("HttpHelper", jsonString);
        RequestBody body = RequestBody.create(JSON, jsonString);
        post(URL_Devices_List,body,callBackClass,DeviceListData.class);
    }


    /**
     *
     */
    public void getDeviceOnOffDetailResult(HttpCallBack<RecordDetail> callback) {
        // String jsonString = "{\"obj\":\"{\\\"deviceMac\\\":\\\"32FFD7055643373914721851\\\"}\"}";
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject.put("deviceMac", "32FFD7055643373914721851");
            jsonObject1.put("obj", jsonObject.toString());
            String jsonString = jsonObject1.toString();
            //String jsonString = "{\"obj\":\"{\\\"deviceMac\\\":\\\"32FFD7055643373914721851\\\"}\"}";
            Log.d("HttpHelperaaa", jsonObject.toString());
            RequestBody body = RequestBody.create(JSON, jsonString);
            post(URL_GetDeviceOnOffDetailResult, body, callback, RecordDetail.class, "GetDeviceOnOffDetailResult");
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    public void getUpdateOpenModeGataWayResult(int i, HttpCallBack callback) throws Exception {
        if (i != 0 && i != 1) {
            throw new Exception("只能输入0或者1");
        }
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject.put("getwayMac", "32FFD7055643373914721851");
            jsonObject.put("control_mode", i + "");
            jsonObject1.put("obj", jsonObject.toString());
            String jsonString = jsonObject1.toString();
            //String jsonString = "{\"obj\":\"{\\\"deviceMac\\\":\\\"32FFD7055643373914721851\\\"}\"}";
            RequestBody body = RequestBody.create(JSON, jsonString);
            post(URL_GetUpdateOpenModeGataWayResult, body, callback, "UpdateOpenModeGataWayResult");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

